export const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

/**
 * Get a random placeholder image from Picsum 
 */
export const getRandomImage = (seed?: string): string => {
  const randomSeed = seed || Math.floor(Math.random() * 1000).toString();
  return `https://picsum.photos/seed/${randomSeed}/400/400`;
}

/**
 * Get a random height for gallery images
 */
export const getRandomImageHeight = (): number => {
  const heights = [240, 280, 320, 360, 400];
  return heights[Math.floor(Math.random() * heights.length)];
}

/**
 * Show toast with loading and success states
 */
export const showLoadingToSuccess = async (
  loadingText: string,
  successText: string,
  duration: number = 1500
): Promise<void> => {
  wx.showLoading({ title: loadingText });
  
  return new Promise((resolve) => {
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: successText,
        icon: 'success',
        duration
      });
      setTimeout(resolve, duration);
    }, 1000);
  });
}
